//
//  SetUpPswView.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/3.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "SetUpPswView.h"
#import "RegularRestrictions.h"
#import "DDTextField.h"
@interface SetUpPswView ()<UITextFieldDelegate,GXJtextFieldDelegate,DDtextfieldDelegate>


@end

@implementation SetUpPswView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSetUpPswView];
    }
    return self;
}

- (void)createSetUpPswView {
    
    
//    self.firstTF = [[GXJTextField alloc] initWithFrame:CGRectMake(74 * m6Scale,  0, 600 * m6Scale, 80 * m6Scale) andRightStr:@"1"];
//    self.firstTF.placeholder = NSLocalString(@"6~20位数字、字母、符号的组合");
//    self.firstTF.font = 28 * m6Scale;
//    self.firstTF.backgroundColor = RGB(243, 243, 243);
//    self.firstTF.layer.borderWidth = 1.0;
//    self.firstTF.layer.borderColor = RGB(203, 203, 203).CGColor;
//    self.firstTF.layer.cornerRadius = 6.0 * m6Scale;
//    self.firstTF.clear = YES;
//    self.firstTF.delegate = self;
//    [self addSubview:self.firstTF];

    self.firstTF = [DDTextField ddTextFieldWithPlaceHolder:NSLocalString(@"6~20位数字、字母、符号的组合") Font:28 * m6Scale textColor:[UIColor blackColor] contentInset:UIEdgeInsetsMake(0, 8, 0, 15) textChangingAction:nil];
    self.firstTF.backgroundColor = RGB(243, 243, 243);
    self.firstTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.firstTF.layer.cornerRadius = 6.0 * m6Scale;
    self.firstTF.layer.borderWidth = 1;
    self.firstTF.showClearButton = YES;
    self.firstTF.delegate = self;
    self.firstTF.isNeedChangeSecureTextEntry = YES;
    self.firstTF.proclaimedImageName = @"显示密码";
    self.firstTF.cryptographImageName = @"隐藏密码02";
    [self addSubview:self.firstTF];
    self.firstTF.frame = CGRectMake(74 * m6Scale,  0, 600 * m6Scale, 80 * m6Scale);
    
//    
//    self.firstBtn = [[UIButton alloc] init];
//    [self.firstBtn setImage:[UIImage imageNamed:@"显示密码"] forState:0];
//    [self.firstBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
//    self.firstBtn.clipsToBounds = YES;
//    [self addSubview:self.firstBtn];
//    [self.firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.firstTF.mas_centerY);
//        make.right.mas_equalTo(self.firstTF.mas_right).offset(-22 * m6Scale);
//        make.size.mas_equalTo(CGSizeMake(40 * m6Scale, 24 * m6Scale));
//    }];

    
    
//    self.secondTF = [[GXJTextField alloc] initWithFrame:CGRectMake(74 * m6Scale, 140 * m6Scale, 600 * m6Scale, 80 * m6Scale) andRightStr:@"1"];
//    self.secondTF.placeholder = NSLocalString(@"请再次输入密码");
//    self.secondTF.font = 28 * m6Scale;
//    self.secondTF.backgroundColor = RGB(243, 243, 243);
//    self.secondTF.layer.borderWidth = 1.0;
//    self.secondTF.layer.borderColor = RGB(203, 203, 203).CGColor;
//    self.secondTF.layer.cornerRadius = 6.0 * m6Scale;
//    self.secondTF.clear = YES;
//    self.secondTF.delegate = self;
//    [self addSubview:self.secondTF];
//    [self.secondTF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.mas_centerX);
//        make.top.mas_equalTo(self.firstTF.mas_bottom).offset(60 * m6Scale);
//        make.size.mas_equalTo(CGSizeMake(600 * m6Scale, 80 * m6Scale));
//    }];
//
    self.secondTF = [DDTextField ddTextFieldWithPlaceHolder:NSLocalString(@"请再次输入密码") Font:28 * m6Scale textColor:[UIColor blackColor] contentInset:UIEdgeInsetsMake(0, 8, 0, 15) textChangingAction:nil];
    self.secondTF.backgroundColor = RGB(243, 243, 243);
    self.secondTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.secondTF.layer.cornerRadius = 6.0 * m6Scale;
    self.secondTF.layer.borderWidth = 1;
    self.secondTF.showClearButton = YES;
    self.secondTF.delegate = self;
    self.secondTF.isNeedChangeSecureTextEntry = YES;
    self.secondTF.proclaimedImageName = @"显示密码";
    self.secondTF.cryptographImageName = @"隐藏密码02";
    [self addSubview:self.secondTF];
    self.secondTF.frame = CGRectMake(74 * m6Scale, 140 * m6Scale, 600 * m6Scale, 80 * m6Scale);
    
//    self.secondBtn = [[UIButton alloc] init];
//    [self.secondBtn setImage:[UIImage imageNamed:@"隐藏密码02"] forState:0];
//    [self.secondBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
//    self.secondBtn.clipsToBounds = YES;
//    [self addSubview:self.secondBtn];
//    [self.secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.secondTF.mas_centerY);
//        make.right.mas_equalTo(self.secondTF.mas_right).offset(-22 * m6Scale);
//        make.size.mas_equalTo(CGSizeMake(40 * m6Scale, 24 * m6Scale));
//    }];

    
    self.sureBtn = [[UIButton alloc] init];
    [self.sureBtn setTitle:NSLocalString(@"完成") forState:0];
    self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:36 * m6Scale];
    [self.sureBtn setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:0];
//    self.sureBtn.backgroundColor = RGB(210, 235, 255);
    self.sureBtn.backgroundColor = APPbuttonDisableColor;
    self.sureBtn.layer.cornerRadius = 10.0 * m6Scale;
    [self.sureBtn addTarget:self action:@selector(clickSureBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.secondTF.mas_bottom).offset(120 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(600 * m6Scale, 80 * m6Scale));
    }];
}

- (void)ddtextFieldChangingText:(DDTextField *)textField {
    if (self.firstTF.text.length > 0 && self.secondTF.text.length > 0) {
        self.sureBtn.enabled = YES;
        self.sureBtn.backgroundColor = APPColor;
    }else {
        self.sureBtn.backgroundColor = APPbuttonDisableColor;
    }
}

//- (void)GXJtextFieldChangeText:(GXJTextField *)textField {
//    if (self.firstTF.text.length > 0 && self.secondTF.text.length > 0) {
//        self.sureBtn.enabled = YES;
//        self.sureBtn.backgroundColor = APPColor;
//    }else {
//        self.sureBtn.backgroundColor = APPbuttonDisableColor;
//    }
//}

- (void)clickSureBtn {
    
    if (self.isResetPassword) {
        if ([self.delegate respondsToSelector:@selector(clickSureBtn)]) {
            [self.delegate clickSureBtn];
        }
    }else {
        if (![RegularRestrictions IsCorrentPWDformatter:self.firstTF.text]) {
            [SVProgressHUD showErrorWithStatus:NSLocalString(@"密码格式不正确,请重新输入")];
            return;
        }
        
        if (![self.firstTF.text isEqualToString:self.secondTF.text]) {
            [SVProgressHUD showErrorWithStatus:NSLocalString(@"两次输入的密码不一致,请重新输入")];
            return;
        }
        
        if ([self.delegate respondsToSelector:@selector(clickSureBtn)]) {
            [self.delegate clickSureBtn];
        }
    }
}


@end
